/**
 *
 *
 * @author   Liu Yutao <koal@vip.qq.com>
 * @date     2018/7/4 14:25
 */
define([
    'jquery',
    'baseUtils',
    'base',
    'msgTool',
    'print',
    'plugins/bootstrap-table/bootstrap-table',
    "css!global/plugins/bootstrap-dialog/bootstrap-dialog.css"
], function ($, baseUtils, base, msgTool, print) {
    //默认加载基础库
    base();

    /**
     * 证件打印
     */
    return {
        list: function (webUrl) {
            /**
             * 实际执行方法
             */
            if(window.isAIO !== undefined && window.isAIO){
                // 单身
                $("#printDanshen").on('click', function () {
                    var IdCardNo = $(this).data("idcard");
                    //第三个参数要与PersonalItemTypeEnum中保持一致
                    executePrint($(this), IdCardNo, "danshen", "婚姻状态查询单");
                });
            }else {
                msgTool.error('当前不是一体机环境！', "错误提示");
            }



            /**
             * 打印证件打印
             * @param submit 提交按钮
             * @param IdCardNo 身份证号码
             * @param itemType 事项类型
             * @param cardName 卡名
             */
            function executePrint(submit, IdCardNo, itemType, cardName) {
                //用户信息是否存在
                $.ajax({
                    url: "/personalRecord/save",
                    type: "POST",
                    data: {
                        IdCardNo: IdCardNo,
                        itemType: itemType
                    },
                    beforeSend: function () {
                        //触发ajax请求开始时执行
                        $(submit).find("b").text('提交中...');
                        $(submit).attr('disabled', 'disabled');
                    },
                    success: function (data) {
                        if (data.success) {
                            var printUrl = webUrl+"/personal/" + itemType + "/show/" + IdCardNo;
                            //执行打印纸张
                            var printData = JSON.stringify({
                                "url": printUrl,
                                "ChoiceTray": 5,
                                "ChoicePaper": 0
                            });
                            send_command(
                                "printUrl2",
                                printData,
                                function (receivedData) {
                                    var info = JSON.parse(receivedData);
                                    if (info.status === 100) {
                                        msgTool.success("恭喜您，打印" + cardName + "成功！", "信息提示");
                                    }
                                    else if (info.status === 300) {
                                        msgTool.error("模块未开启", "错误提示");
                                    }
                                    else {
                                        msgTool.error("打印失败，" + info.msg, "错误提示");
                                    }
                                }
                            );

                        } else {
                            msgTool.error(data.msg);
                        }
                    },
                    error: function () {
                        msgTool.error("操作失败，请刷新当前页面后重试！");
                    },
                    complete: function () {
                        setTimeout(function () {
                            $(submit).find("b").text("打印" + cardName);
                            $(submit).removeAttr('disabled');
                        }, 5000);
                    }
                });
            }
          
        },
        laonianCard:function(cardDataJson){
            var IdCardNo = $(this).data("idcard");
             //第三个参数要与PersonalItemTypeEnum中保持一致
            executePrintCard($("#printLaonianCard"), IdCardNo, "laonianCard", "老年证（卡片版）");
            /**
             * 老年证信息校验
             * @param cardDataJson  老年卡信息JOSN
             */
          function checkcardData(cardDataJson){
            	var flag={"status":true,"info":""};
            	if(cardDataJson.image[0].imgstr==''){
            		flag.status=false;
            		flag.info="老年证头像生成异常";
            	}
            	if(cardDataJson.image[1].imgstr==''){
            		flag.status=false;
            		flag.info="老年证签章生成异常";
            	}
            /*	if(cardDataJson.image[2].imgstr==''){
            		flag.status=false;
            		flag.info="老年证二维码生成异常";
            	}*/
            	return flag;
            }
          
            /**
             * 打印老年证实体卡
             * @param submit 提交按钮
             * @param IdCardNo 身份证号码
             * @param itemType 事项类型
             * @param cardName 卡名
             */
            function executePrintCard(submit, IdCardNo, itemType, cardName) {
            	var flag={"status":true,"info":""};
            	flag=checkcardData(cardDataJson);
            	if(flag.status){
                msgTool.success("正在获取打印机信息", "提示");
                send_command("CardPrinter_getStatus", "", function (receivedData) {
                    var info = JSON.parse(receivedData);
                    if (info.status === 100) {
                        if (info.inkRibbon === 0) {
                            msgTool.error("色带已耗尽，请联系工作人员解决！", "错误提示");
                        } else {
                            msgTool.success("色带剩余量：" + info.inkRibbon, "提示");
                            //开始打印
                            $.ajax({
                                url: "/personalRecord/save",
                                type: "POST",
                                async:false,
                                data: {
                                    IdCardNo: IdCardNo,
                                    itemType: itemType
                                },
                                beforeSend: function () {
                                    //触发ajax请求开始时执行
                                    $(submit).find("b").text('准备打印...');
                                    $(submit).attr('disabled', 'disabled');
                                },
                                success: function (data) {
                                    if (data.success) {
                                        //执行打印实体卡
                                        var configJSON = null;
                                        try {
                                            configJSON = cardDataJson;
                                        } catch (err) {
                                            msgTool.error("请检查JSON是否合法！", "错误提示");
                                            return
                                        }
                                        if (typeof configJSON == 'object') {
                                            var datajson = JSON.stringify(configJSON);
                                            msgTool.success("正在打印！", "提示");
                                            $(submit).find("b").text('正在打印...');
                                            send_command(
                                                "CardPrinter_print",
                                                datajson,
                                                function (receivedData) {
                                                    var info = JSON.parse(receivedData);
                                                    if (info.status === 100) {
                                                        msgTool.success("打印成功！", "提示");
                                                    } else if (info.status === 300) {
                                                        msgTool.error("模块未开启，请联系工作人员解决！", "错误提示");
                                                    } else if (info.status === 200) {
                                                        Toast('err', '打印失败：' + info.msg);
                                                        msgTool.error("打印失败：" + info.msg, "错误提示");
                                                    }
                                                }
                                            );
                                        }

                                    } else {
                                        msgTool.error(data.msg);
                                    }
                                },
                                error: function () {
                                    msgTool.error("操作失败，请刷新当前页面后重试！");
                                },
                                complete: function () {
                                    setTimeout(function () {
                                        $(submit).find("b").text("已打印" + cardName);
                                        $(submit).removeAttr('disabled');
                                    }, 30000);
                                }
                            });
                        }
                    } else if (info.status === 300) {
                        msgTool.error("模块未开启，请联系工作人员解决！", "错误提示");
                    } else if (info.status === 200) {
                        msgTool.error("获取失败：" + info.msg, "错误提示");
                    }
                });

            	}else{
            		   msgTool.error(flag.info+"，请联系管理员！", "错误提示");
            	}
            }
            
            /**
             * 获取实体卡打印机状态
             */
            function card_status() {
                var status = false;
                msgTool.success("正在获取打印机信息", "提示");
                send_command("CardPrinter_getStatus", "", function (receivedData) {
                    var info = JSON.parse(receivedData);
                    if (info.status === 100) {
                        if (info.inkRibbon === 0) {
                            msgTool.error("色带已耗尽，请联系工作人员解决！", "错误提示");
                        } else {
                            msgTool.success("色带剩余量：" + info.inkRibbon, "提示");
                            status = true;
                        }
                    } else if (info.status === 300) {
                        msgTool.error("模块未开启，请联系工作人员解决！", "错误提示");
                    } else if (info.status === 200) {
                        msgTool.error("获取失败：" + info.msg, "错误提示");
                    }
                });
                return status;
            }

            
            
        }
    };
});
